#include <iostream>
#include <string>
#include <map>
using namespace std;

map<string,int> mp;
int b;

bool low(char x){
	if((x >= '0' && x <= '9')||(x >= 'a' && x <= 'z')||(x >= 'A' && x <= 'Z')){
		return true;
	}
	else return false;
}

int main(){
	int n,p;
	cin >> n >> p;
	while(n--){
		string str;
		int b;
		cin >> str >> b;
		mp[str] = b;
	}
	char ch;
	string s;
	while(scanf("%c",&ch) != EOF){
		if(low(ch)){
			s += ch;
		}
		else{
			b += mp[s];
			s = "";
		}
	}
	cout << b % p << endl;
	return 0;
} 
